package com.cee.admin.modules.cc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.common.core.query.BasePageQuery;
import com.cee.common.core.vo.SimpleSelectionBox;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.cc.entity.CcEmissionSource;
import com.cee.domain.modules.cc.form.add.CcEmissionSourceAddForm;
import com.cee.domain.modules.cc.form.update.CcEmissionSourceUpdateForm;
import com.cee.domain.modules.cc.vo.CcEmissionFactorVo;
import com.cee.domain.modules.cc.vo.CcEmissionSourceVo;
import com.cee.ds.common.constants.PermissionConstants;
import com.cee.ds.modules.cc.service.ICcEmissionSourceService;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.cee.admin.common.BaseController;

import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 排放源 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2023-12-22
 */
@RestController
@RequestMapping("/api/v1/carbonComponent/carbonEmissionSource")
@Tag(name = "碳组件—排放源")
public class CcEmissionSourceController extends BaseController {

    @Autowired
    private ICcEmissionSourceService emissionSourceService;

    @Autowired
    private ICcEmissionSourceService ccEmissionSourceService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_PREFIX + PermissionConstants.READ)
    public Result<Page<CcEmissionSourceVo>> page(BasePageQuery<CcEmissionSourceVo> query, String name, Integer categoryId, Integer originId) {
        CcEmissionSourceVo vo = new CcEmissionSourceVo();
        vo.setName(name);
        vo.setCategoryId(categoryId);
        vo.setOriginId(originId);
        return success(emissionSourceService.queryPageVo(query.generatePage(), vo));
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_PREFIX + PermissionConstants.ADD)
    public Result<Boolean> add(@RequestBody @Valid CcEmissionSourceAddForm addForm) {
        return success(emissionSourceService.save(addForm.toEntity()));
    }

    @PostMapping("/update")
    @Operation(summary = "更新")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_PREFIX + PermissionConstants.UPDATE)
    public Result<Boolean> update(@RequestBody @Valid CcEmissionSourceUpdateForm updateForm) {
        return success(emissionSourceService.updateById(updateForm.toEntity()));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_PREFIX + PermissionConstants.DEL)
    public Result<Boolean> delete(@PathVariable("id") Integer id)  {
        return success(emissionSourceService.removeById(id));
    }

    @GetMapping("/selectionBox")
    @Operation(summary = "下拉框数据", description = "查询条件：originId(非必须)")
    public Result<List<SimpleSelectionBox>> selectionBox(Integer originId) {
        return success(emissionSourceService.selectionBox(originId));
    }

    @GetMapping("/all/list")
    @Operation(summary = "获取所有排放源集合")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_PREFIX + PermissionConstants.READ)
    public Result<List<CcEmissionSource>> listAdd() {
        return success(emissionSourceService.list());
    }

    @GetMapping("/all/listFuel")
    @Operation(summary = "获取所有化石燃料排放源集合")
    @RequiresPermissions(PermissionConstants.CC_EMISSIONS_PREFIX + PermissionConstants.READ)
    public Result<List<CcEmissionSource>> listFuel() {
        // 加载出排放源源流类型为 化石燃料 的排放源
        return success(emissionSourceService.lambdaQuery().eq(CcEmissionSource::getOriginId,1).list());
    }

    @GetMapping("/getFuelGas")
    @Operation(summary = "获取所有燃料气低位发热量缺省值", description = "k=燃料气名称，v=低位发热量缺省值")
    public Result<Map<String, Double>> getFuelGas() {
        return success(emissionSourceService.selectFuelGas().stream().collect(Collectors.toMap(CcEmissionFactorVo::getSourceName, CcEmissionFactorVo::getNetCalorificValue)));
    }

    @PostMapping("/getEmSourceByOriginId")
    @Operation(summary = "通过排放源流ID查询排放源填充下拉框", description = "originId：排放源源流ID")
    public Result<List<SimpleSelectionBox>> getEmSourceByOriginId(@Parameter(description = "排放源流ID集合") @RequestBody Collection<Integer> originIds) {
        return success(ccEmissionSourceService.selectionBox(originIds));
    }

}

